package 力扣._100032_使数组为空的最少操作次数;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Solution {
    public int minOperations(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num))
                map.put(num, map.get(num) + 1);
            else
                map.put(num, 1);
        }
        int total = 0;
        //模3，模2 若 num = 3x + 2y ,则可以，这时让x尽可能大
        //否则，不可为空
        Set<Integer> set = map.keySet();
        for (Integer num : set) {
            int count = map.get(num);
            if (count % 3 == 0)
                total += count / 3;
            else if (count % 3 == 2)
                total += count / 3 + 1;
            else if (count == 1)
                return -1;
            else {
                total += count / 3 - 1;
                total += (count - (count / 3 - 1) * 3) / 2;
            }
        }
        return total;
    }

    public static void main(String[] args) {
        int[] t = new int[]{2, 3, 3, 2, 2, 4, 2, 3, 4};
        int i = new Solution().minOperations(t);
        System.out.println(i);
    }
}
